package androidgames.networkgames.screens.components;

import java.util.ArrayList;

import com.badlogic.androidgames.framework.Input.*;
import com.badlogic.androidgames.framework.Graphics;
import com.badlogic.androidgames.framework.Pixmap;
import com.badlogic.androidgames.framework.Screen;

public abstract class DrawableComponent {
	
	public class CEvent
	{
		public static final int TOUCH = 0;
		public static final int DOUBLE_TOUCH = 1;
		public static final int DRAG = 2;
		public static final int DROP = 3;
		public static final int TOUCH_DOWN = 4;
		public static final int LONG_TOUCH = 5;
		
		public int iEventType;
		public int iX,iY;
		public int iOldX, iOldY;
	}
	
	public static interface IEventListener {
        public void onTouch(DrawableComponent view, CEvent event);
    }
    
    private ArrayList<IEventListener> onTouchListeners = new ArrayList<IEventListener>();

    public void addTouchListener(IEventListener listener) {
    	onTouchListeners.add(listener);
    }
    
    public void removeTouchListener(IEventListener listener) {
    	onTouchListeners.remove(listener);
    }
    
    public void fireEvent(CEvent event) {
    	IEventListener[] listeners = new IEventListener[1];
    	listeners = onTouchListeners.toArray(listeners);
    	if(listeners.length < 0)
    		return;
    	for(IEventListener listener : listeners)
    	{
    		listener.onTouch(this, event);
    	}
    }

	public Pixmap background;
	public int iBackgroundColor;
	public int iX;
	public int iY;
	public int iSourceX;
	public int iSourceY;
	public int iWidth;
	public int iHeight;
	public Screen screen;
	
	public DrawableComponent(Screen screen, Pixmap background, int iWidth, int iHeight, int iX, int iY, int iSX, int iSY) {
		this.screen = screen;
		this.background = background;
		this.iWidth = iWidth;
		this.iHeight = iHeight;
		this.iX = iX;
		this.iY = iY;
		this.iSourceX = iSX;
		this.iSourceY = iSY;
	}
	
	public DrawableComponent(Screen screen, int iBackgroundColor, int iWidth, int iHeight, int iX, int iY, int iSX, int iSY) {
		this.screen = screen;
		this.iBackgroundColor = iBackgroundColor;
		this.iWidth = iWidth;
		this.iHeight = iHeight;
		this.iX = iX;
		this.iY = iY;
		this.iSourceX = iSX;
		this.iSourceY = iSY;
	}
	
	public boolean isTouched(int iTouchX, int iTouchY) {
		if(iTouchX >= iX && iTouchX <= iX + iWidth && iTouchY >= iY && iTouchY <= iY + iHeight)
			return true;
		return false;
	}
	
	public abstract boolean update(float deltatime, TouchEvent touchevent);
	public abstract void present(float deltatime, Graphics g);
}
